﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Runda.EShop.Common.Entity;
using Runda.EShop.Bll;
using Runda.EShop.Common.Utility;
using System.Data;

namespace Runda.EShop.SaaS.CustomerManage
{
    public partial class CustomerAdd : BasePage
    {
        #region 属性

        public Guid CustomerID
        {
            get
            {
                if (ViewState["CustomerID"] == null)
                    return Guid.Empty;
                else
                    return new Guid(ViewState["CustomerID"].ToString());
            }
            set { ViewState["CustomerID"] = value; }
        }

        public bool IsAdd
        {
            get { return Convert.ToBoolean(ViewState["IsAdd"].ToString()); }
            set { ViewState["IsAdd"] = value; }
        }

        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitForm();

                if (!string.IsNullOrEmpty(Request.QueryString["CustomerID"]))
                {
                    IsAdd = false;
                    CustomerID = new Guid(Request.QueryString["CustomerID"]);
                    SetFormData();
                    trPwd.Visible = false;
                }
                else
                {
                    IsAdd = true;
                    CustomerID = Guid.NewGuid();
                }
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            string Error = "";
            if (!TrySave(ref Error))
            {
                ClientScript.RegisterStartupScript(this.GetType(), "error", "alert('" + Error + "')", true);
                return;
            }

            ClientScript.RegisterStartupScript(this.GetType(), "SaveAndClose", "OnSaved();", true);
        }

        private void InitForm()
        {

        }

        private bool TrySave(ref string Message)
        {
            SystemBll bll = new SystemBll();
            BizCustomerEntity entity = null; 

            //检查账号是否重复
            List<BizCustomerEntity> entityList = new List<BizCustomerEntity>();
            if (IsAdd)
            {
                List<SqlDbParameter> parms = new List<SqlDbParameter>();
                SqlDbParameter parm = new SqlDbParameter();
                parm.ColumnName = "CustomerCode";
                parm.ColumnType = DbType.String;
                parm.ParameterName = "@CustomerCode";
                parm.ParameterValue = txtAccount.Text.Trim();
                parm.QualificationType = SqlDbParameter.QualificationSymbol.Equal;
                parms.Add(parm);

                entityList = bll.GetAll<BizCustomerEntity>(parms);
                if (entityList.Count > 0)
                {
                    Message = "您输入的帐号已存在";
                    return false;
                }
            }
            else
            {
                List<SqlDbParameter> parms = new List<SqlDbParameter>();
                SqlDbParameter parm = new SqlDbParameter();
                parm.ColumnName = "CustomerCode";
                parm.ColumnType = DbType.String;
                parm.ParameterName = "@CustomerCode";
                parm.ParameterValue = txtAccount.Text.Trim();
                parm.QualificationType = SqlDbParameter.QualificationSymbol.Equal;
                parms.Add(parm);

                parm = new SqlDbParameter();
                parm.ColumnName = "CustomerID";
                parm.ColumnType = DbType.Guid;
                parm.ParameterName = "@CustomerID";
                parm.ParameterValue = CustomerID;
                parm.QualificationType = SqlDbParameter.QualificationSymbol.NotEqual;
                parms.Add(parm);

                entityList = bll.GetAll<BizCustomerEntity>(parms);
                if (entityList.Count > 0)
                {
                    Message = "您输入的帐号已存在";
                    return false;
                }
            }

            //保存
            if (!IsAdd)
            {
                entity = bll.Get<BizCustomerEntity>(CustomerID);
            }
            else
            {
                entity = new BizCustomerEntity();
                entity.CustomerID = CustomerID;
                //entity.CreatedAt = DateTime.Now;
                //entity.CreatedBy = base.User.Identity.LoginUser.UserID;
                //entity.CreatedName = base.User.Identity.LoginUser.UserName;
                entity.EntID = new Guid(Request.QueryString["EntID"]); 
                entity.Password = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(txtPwd.Text.Trim(), "MD5");
            }
            entity.CustomerCode = txtAccount.Text;
            entity.CustomerName = txtUserName.Text;
            entity.DeptName = txtDeptName.Text;

            bll.InsertOrUpdate(entity);
            IsAdd = false;
            return true;

        }

        private void SetFormData()
        {
            if (CustomerID != Guid.Empty)
            {
                BizCustomerEntity entity = null;
                SystemBll bll = new SystemBll();
                entity = bll.Get<BizCustomerEntity>(CustomerID);

                txtAccount.Text = entity.CustomerCode;
                txtUserName.Text = entity.CustomerName;
                txtDeptName.Text = entity.DeptName;

            }
        }

    }
}